import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, App } from 'ionic-angular';
// import { WarnPage } from '../warn/warn';
import { WarnService } from '../warn/services/warnService.service';
import { AlertController } from 'ionic-angular';

import { ModalController } from 'ionic-angular/components/modal/modal-controller';
import { TabsPage } from '../tabs/tabs';
import { WarnPage } from '../warn/warn'
import * as moment from 'moment';
import * as $ from 'jquery';


/**
 * Generated class for the WarnDetailsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-warn-details',
  templateUrl: 'warn-details.html',
})
export class WarnDetailsPage {
  id: string;
  data: any;
  data1: any[];
  // time: any;

  sensorName: any;
  sensorType: any;
  message: any;
  level: any;
  status: any;
  remarks: any;
  textarea1: any;
  inputBlooean = false;
  userId: any;
  warnId: any;
  start: any;

  nowDate: any;

  targetName: any;
  targetCode: any;
  time: any;

  name;
  warntime;
  name1;
  colorName;
  lower;
  upper;
  message1;
  status1;


  constructor(
    private app: App,
    public alertCtrl: AlertController,
    public modalCtrl: ModalController,
    public warnService: WarnService,
    public navCtrl: NavController, public navParams: NavParams) {

    this.warnId = this.navParams.get('warnId');  //warm页面传过来
    this.warntime = this.navParams.get('time');
    this.name = this.navParams.get('name');
    this.name1 = this.navParams.get('name1');
    this.colorName = this.navParams.get('colorName');
    this.lower = this.navParams.get('lower');
    this.upper = this.navParams.get('upper');
    this.message1 = this.navParams.get('message');
    this.status1 = this.navParams.get('status');

    console.log(11, this.warnId);
    // console.log('WarnDetailsPage.id:', this.navParams.get('id'));

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad WarnDetailsPage');



    // this.userId = sessionStorage.getItem('username');
    this.id = sessionStorage.getItem('token');   //拿到内存中 userid
    console.log(22, this.id);
    this.warnService.queryByIdRemarks(this.warnId)
      .subscribe(data => {
        if (data.length == 0) { return }

        console.log(66666, data);
        this.data1 = data;
        console.log(111, this.data);
        this.remarks = this.data1[0].remarks;
        console.log(667, this.remarks);

      }, err => console.error(err));

  }


  inputText() {
    if (this.remarks.length != 0) {
      $(".btn").removeAttr("disabled")
    } else {
      $(".btn").attr("disabled", "disabled")
    }
  }

  update() {
    this.nowDate = new Date().toLocaleString();
    let index = this.nowDate.indexOf("午");
    this.start = moment(this.nowDate.slice(0, index - 1) + this.nowDate.slice(index + 1), "YYYY/M/D HH:mm:ss").format("YYYYMMDDHHmmssSSS");
    console.log(5555555, this.start);
    const warn = Object.assign({ remarks: this.remarks }, { status: 1 });
    console.log(666, warn);
    this.warnService
      .update(warn,this.warnId,this.id)
      .subscribe();
    this.showAlert();
  }

  // 弹框并跳转
  showAlert() {
    let alert = this.alertCtrl.create({
      title: '处理信息',
      subTitle: '处理意见已提交',
      buttons: [
        {
          text: '确定',
          handler: () => {
            //  this.navCtrl.push(WarnPage);
            //  this.modalCtrl.create(WarnPage);
            // let modal = this.modalCtrl.create('WarnPage');
            // this.navCtrl.pop();
            // modal.present();
            // this.navCtrl.parent.select(2); 
            // this.navCtrl.parent.select(1);
            // this.app.getRootNav().setRoot(WarnPage);
            this.navCtrl.setRoot(TabsPage);
          },
        }
      ]
    });

    alert.present();
  }

  // inputText() {
  //   var a = this.textarea1.length;
  //   if (a > 0) {
  //     this.inputBlooean = true;
  //   } else {
  //     this.inputBlooean = false;
  //   }
  // }




}
